package sk.fei.vrml.objects;

import sk.fei.vrml.main.Constants;
import sk.fei.vrml.main.Transformator;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;

/**
 * VrmlBox class represents VRML object Box. Only properties of VRML Box is
 * y, x and z.
 * 
 * http://www.lighthouse3d.com/vrml/tutorial/index.shtml?box
 * 
 * Box {
 *		field SFVec3f size 2 2 2
 * }
 * 
 * @author anubis
 * 
 */
public class VrmlBox extends VrmlObject {
	private float x;
	private float y;
	private float z;

	public VrmlBox(float x, float y, float z) {

		this.type = Constants.Box;
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public void draw(GLAutoDrawable GLAutoDrawable, VrmlAppearance appearance) {
		GL gl = GLAutoDrawable.getGL();
		GLU glu = new GLU();
		if(appearance != null) {
			appearance.applyObjectMaterial(GLAutoDrawable);
			appearance.applyCubeTexture(GLAutoDrawable);
		}
		gl.glBegin(GL.GL_QUADS);
		
		// predna stena
		gl.glNormal3d(0, 0, 1);
	
		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f((this.x / 2),-(this.y / 2),(this.z / 2));
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3f((this.x / 2), (this.y / 2),(this.z / 2));
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3f(- (this.x / 2),(this.y / 2),(this.z / 2));
		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f(- (this.x / 2),- (this.y / 2), (this.z / 2));
		
		// lava stena
		gl.glNormal3d(-1, 0, 0);
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3f(- (this.x / 2),(this.y / 2),(this.z / 2));
		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f(- (this.x / 2),- (this.y / 2),(this.z / 2));
		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f(- (this.x / 2),- (this.y / 2),- (this.z / 2));
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3f(- (this.x / 2),(this.y / 2),- (this.z / 2));
		
		// zadna stena
		gl.glNormal3d(0, 0, -1);
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3f((this.x / 2),(this.y / 2),- (this.z / 2));
		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f((this.x / 2),- (this.y / 2),- (this.z / 2));
		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f(- (this.x / 2),- (this.y / 2),- (this.z / 2));
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3f(- (this.x / 2),(this.y / 2),- (this.z / 2));
		
		// prava stena
		gl.glNormal3d(1, 0, 0);
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3f((this.x / 2),(this.y / 2),(this.z / 2));
		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f((this.x / 2),- (this.y / 2),(this.z / 2));
		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f((this.x / 2),- (this.y / 2),- (this.z / 2));
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3f((this.x / 2),(this.y / 2),- (this.z / 2));
		
		// spodna stena
		gl.glNormal3d(0, -1, 0);
		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f(- (this.x / 2),- (this.y / 2),- (this.z / 2));
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3f(- (this.x / 2),- (this.y / 2),(this.z / 2));
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3f((this.x / 2),- (this.y / 2),(this.z / 2));
		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f((this.x / 2),- (this.y / 2),- (this.z / 2));
		
		// horna stena
		gl.glNormal3d(0, 1, 0);
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3f(- (this.x / 2), (this.y / 2),- (this.z / 2));
		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f(- (this.x / 2),(this.y / 2),(this.z / 2));
		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f((this.x / 2),(this.y / 2),(this.z / 2));
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3f((this.x / 2),(this.y / 2),- (this.z / 2));
		gl.glEnd();
		
		if(appearance != null ) {
        	if (appearance.isTextureSeted()) gl.glDisable(GL.GL_TEXTURE_2D);
        }
	}

	public String toString() {
		return "Cube";
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public float getZ() {
		return z;
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = y;
	}

	public void setZ(float z) {
		this.z = z;
	}

}
